package com.yss.sofa.report.engine.cell.invisible.dataset;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yss.sofa.report.engine.Const;
import com.yss.sofa.report.engine.Context;
import com.yss.sofa.report.engine.DataSet;
import com.yss.sofa.report.engine.DataSetData;
import com.yss.sofa.report.engine.cell.ReportCell;
import com.yss.sofa.report.engine.cell.invisible.InvisibleCell;


public class VirtualDataSetCell extends InvisibleCell implements DataSet{
	//----------------------------------------------------------------------------------------------
	private class VirtualDataSetData extends DataSetData{
		private static final long serialVersionUID = 2691691171192391154L;
		private VirtualDataSetCell dataset;
		private final Map<String,String> record=new HashMap<String,String>();
		public VirtualDataSetData(VirtualDataSetCell dataset){
			super();
			this.dataset=dataset;
			for(String field:this.dataset.getFields()){
				this.addField(field);
			}
			for(String f:this.getFields()){
				record.put(f, "");
			}
		}
		public int getCount(){
			return dataset.length;
		}
		public int size(){
			return this.getCount();
		}
		public String getValue(int index,CharSequence field) throws Exception{
			if(FIELD_NONE.equalsIgnoreCase(field.toString().toLowerCase())){
				return "";
			}
			if(this.getFields().contains(field.toString().toLowerCase())){
				return "";
			}
			throw new Exception("字段["+field+"]不存在");
		}
		public Map<String,String> getRecord(int index){
			if(index<0 || index>=dataset.length){
				throw new IndexOutOfBoundsException("Index: "+index+", Size: "+dataset.length);
			}
			return record;
		}
	}
	//----------------------------------------------------------------------------------------------
	private static final long serialVersionUID = 2146973930245320177L;
	private static final Logger LOGGER=LoggerFactory.getLogger(VirtualDataSetCell.class);
	private String count;
	private int length;
	private String exec;
	private List<String> fields=new LinkedList<String>();
	protected void init() {
		super.init();
		this.type=Const.REPORT_CELL_TYPE_VIRTUAL_DATASET;
		this.count="0";
		this.length=0;
		this.exec="true";
	}
	public void assignTo(ReportCell cell){
		VirtualDataSetCell _=(VirtualDataSetCell)cell;
		super.assignTo(_);
		_.count=this.count;
		_.length=this.length;
		_.exec=this.exec;
		_.fields.clear();
		_.fields.addAll(this.fields);
	}
	public VirtualDataSetCell create(){
		VirtualDataSetCell _=new VirtualDataSetCell();
		this.assignTo(_);
		return _;
	}
	public Set<String> getParams() {
		return null;
	}

	public DataSetData execute(String classname,Context context,Map<String,Serializable> params,Map<String,String> consts) throws Exception {
		if(length>10000){
			LOGGER.warn("虚拟数据集["+code+"]返回的数据已超过10000条");
		}
		return new VirtualDataSetData(this);
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public List<String> getFields() {
		return fields;
	}

	public void setFileds(List<String> fileds) {
		this.fields = fileds;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getLength() {
		return length;
	}
	public String getExec() {
		return exec;
	}
	public void setExec(String exec) {
		this.exec = exec;
	}
}
